/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package defaultPackage;

import entityBeans.UserEntity;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import sessionBeans.UserSessionBeanLocal;
import utilities.HashFunctions;

/**
 *
 * @author h08182
 */
@ManagedBean(name = "jarjarbean")
@SessionScoped
public class UserManagedBean {

    private String email;
    private String password;
    private String password1;
    private String password2;
    private String pseudo;
    private String name;
    private String firstname;
    private String status;
    private String signature;
    @EJB
    private UserSessionBeanLocal userEJB;
    private UserEntity userConnected;
    private String errorEmail;
    private String errorPass;
    private String errorPass1;
    private String errorPass2;
    private String errorPseudo;
    private String errorName;
    private String errorFirstName;
    private String errorLogin;

    public String getSignature() {
        return signature;
    }

    public void setSignature(String signature) {
        this.signature = signature;
    }

    public String getErrorPass() {
        return errorPass;
    }

    public void setErrorPass(String errorPass) {
        this.errorPass = errorPass;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getErrorLogin() {
        return errorLogin;
    }

    public void setErrorLogin(String errorLogin) {
        this.errorLogin = errorLogin;
    }

    public String getErrorEmail() {
        return errorEmail;
    }

    public void setErrorEmail(String errorEmail) {
        this.errorEmail = errorEmail;
    }

    public String getErrorPass1() {
        return errorPass1;
    }

    public void setErrorPass1(String errorPass1) {
        this.errorPass1 = errorPass1;
    }

    public String getErrorPass2() {
        return errorPass2;
    }

    public void setErrorPass2(String errorPass2) {
        this.errorPass2 = errorPass2;
    }

    public String getErrorPseudo() {
        return errorPseudo;
    }

    public void setErrorPseudo(String errorPseudo) {
        this.errorPseudo = errorPseudo;
    }

    public String getErrorName() {
        return errorName;
    }

    public void setErrorName(String errorName) {
        this.errorName = errorName;
    }

    public String getErrorFirstName() {
        return errorFirstName;
    }

    public void setErrorFirstName(String errorFirstName) {
        this.errorFirstName = errorFirstName;
    }

    public UserEntity getUserConnected() {
        return userConnected;
    }

    public void setUserConnected(UserEntity userConnected) {
        this.userConnected = userConnected;
    }

    public UserManagedBean() {
        this.userEJB = null;
        this.userConnected = null;
    }

    public UserSessionBeanLocal getUserEJB() {
        return userEJB;
    }

    public void setUserEJB(UserSessionBeanLocal userEJB) {
        this.userEJB = userEJB;
    }

    /**
     * Get the value of email
     *
     * @return the value of email
     */
    public String getEmail() {
        return email;
    }

    /**
     * Set the value of email
     *
     * @param email new value of email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * Get the value of pseudo
     *
     * @return the value of pseudo
     */
    public String getPseudo() {
        return pseudo;
    }

    /**
     * Set the value of pseudo
     *
     * @param pseudo new value of pseudo
     */
    public void setPseudo(String pseudo) {
        this.pseudo = pseudo;
    }

    /**
     * Get the value of password1
     *
     * @return the value of password1
     */
    public String getPassword1() {
        return password1;
    }

    /**
     * Set the value of password1
     *
     * @param password1 new value of password1
     */
    public void setPassword1(String password1) {
        this.password1 = password1;
    }

    /**
     * Get the value of password2
     *
     * @return the value of password2
     */
    public String getPassword2() {
        return password2;
    }

    /**
     * Set the value of password2
     *
     * @param password2 new value of password2
     */
    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    /**
     * Get the value of name
     *
     * @return the value of name
     */
    public String getName() {
        return name;
    }

    /**
     * Set the value of name
     *
     * @param name new value of name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * Get the value of firstname
     *
     * @return the value of firstname
     */
    public String getFirstname() {
        return firstname;
    }

    /**
     * Set the value of firstname
     *
     * @param firstname new value of firstname
     */
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    /**
     * Get the value of status
     *
     * @return the value of status
     */
    public String getStatus() {
        return status;
    }

    /**
     * Set the value of status
     *
     * @param status new value of status
     */
    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * Allows a user to login or not, just checking if he is in the database
     *
     * @return the page to go to (have a look at face-config.xml)
     */
    public String login() {
        // the password from the form, hashed in MD5
        password1 = HashFunctions.stringToMD5(password1);

        userConnected = userEJB.login(email, password1);
        System.out.println(userConnected.getEmail());
        // if userConnected is null, then an error occured, most likely no match found
        errorLogin = (userConnected == null) ? "L'association adresse email / mot de passe est incorrecte" : null;
        return "reload";
    }

    /**
     * Register a user -- Handle the errors and redirect to the right page
     *
     * @return the page to go to (have a look at face-config.xml)
     */
    public String register() {
//        Pattern patternEmail = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$");
        Pattern patternEmail = Pattern.compile("^[A-Z0-9._%+-]+@HEI.FR$");
        Matcher matcherEmail = patternEmail.matcher(email.toUpperCase());

        // if the email address has an email format
            // test if email address already in database
        if (!matcherEmail.matches()){
            errorEmail = "L'adresse email n'est pas conforme";
        }else if (userEJB.testNewUserMail(email)){
            errorEmail = "Cet email est déjà associé à un compte.";
        }else{
            errorEmail = null;
        }
        // password not empty
        errorPass1 = (password1.equals("")) ? "Veuillez entrer un mot de passe" : null;
        // pseudo not empty
        errorPseudo = (pseudo.equals("")) ? "Veuillez entrer un pseudo" : null;
        // name not empty
        errorName = (name.equals("")) ? "Veuillez entrer votre nom" : null;
        // firstname not empty
        errorFirstName = (firstname.equals("")) ? "Veuillez entrer votre prénom" : null;
        // 2 passwords are the same
        errorPass2 = (!password1.equals(password2)) ? "Les mots de passes sont différents" : null;
        // test if pseudo is already taken
        errorPseudo = (userEJB.testNewUserPseudo(pseudo)) ? "Ce pseudo est déjà associé à un compte." : null;

        // if we have an error
        if (errorEmail != null || errorFirstName != null || errorName != null
                || errorPass1 != null || errorPass2 != null || errorPseudo != null) {
            return "reload";
        }
        userConnected = userEJB.register(email, HashFunctions.stringToMD5(password1), pseudo, name, firstname);

        // if userConnected is null, then an error occured
        return (userConnected != null) ? "OK" : null;
    }

    /**
     * Logout the user
     *
     * @return the page to go to (have a look at face-config.xml)
     */
    public String logOut() {
        userConnected = null;
        return "reload";
    }

    /**
     * Initialize the different attributes of the User
     *
     * @return the page to go to (have a look at face-config.xml)
     */
    public String goToEditMyAccount() {
        email = userConnected.getEmail();
        password1 = userConnected.getPassword();
        password2 = password1;
        pseudo = userConnected.getPseudo();
        name = userConnected.getName();
        firstname = userConnected.getFirstName();

        return "editMyAccount";
    }

    /**
     * Edit the user details
     *
     * @return the page to go to (have a look at face-config.xml)
     */
    public String editMyAccount() {
        // pseudo not empty
        errorPseudo = (pseudo.equals("")) ? "Veuillez entrer un pseudo" : null;
        // name not empty
        errorName = (name.equals("")) ? "Veuillez entrer votre nom" : null;
        // firstname not empty
        errorFirstName = (firstname.equals("")) ? "Veuillez entrer votre prénom" : null;
        // test if pseudo is already taken
        errorPseudo = (!userEJB.testNewUserPseudoUpdate(userConnected.getId(), pseudo)) ? "Ce pseudo est déjà associé à un compte." : null;
        // if we have an error
        if (errorFirstName != null || errorName != null || errorPseudo != null) {
            return "reload";
        }
        userConnected = userEJB.editMyAccount(userConnected, pseudo, name, firstname);
        return "goToIndex";
    }

    /**
     * Edit the password -- Handle the possible errors
     *
     * @return the page to go to (have a look at face-config.xml)
     */
    public String editMyPassword() {
        // the password from the form, hased in MD5
        String formMD5Password = HashFunctions.stringToMD5(password);

        // check if the user knows the old password
        errorPass = (!formMD5Password.equals(userConnected.getPassword())) ? "Le mot de passe est incorrect" : null;
        // password not empty
        errorPass1 = (password1.equals("")) ? "Veuillez entrer un mot de passe" : null;
        // 2 passwords are the same
        errorPass2 = (!password1.equals(password2)) ? "Les mots de passes sont différents" : null;

        if (errorPass1 != null || errorPass2 != null || errorPass != null) {
            return "reload";
        }
        userConnected = userEJB.editMyPassword(userConnected, HashFunctions.stringToMD5(password1));
        return "goToIndex";
    }

    public String editMySignature() {
        userConnected = userEJB.editMySignature(userConnected, signature);
        return "goToIndex";
    }
}
